package com.rome.hotel.data.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.rome.hotel.data.domain.HotelReserve;
import com.rome.hotel.data.domain.HotelUser;
import com.rome.hotel.data.mapper.HotelReserveMapper;
import com.rome.hotel.data.mapper.HotelUserMapper;
import com.rome.hotel.data.service.IHotelReserveService;
import com.rome.hotel.data.service.IHotelUserService;
import io.jsonwebtoken.lang.Assert;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rome.hotel.data.mapper.HotelOrderMapper;
import com.rome.hotel.data.domain.HotelOrder;
import com.rome.hotel.data.service.IHotelOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单Service业务层处理
 *
 * @author rome
 * @date 2024-02-01
 */
@Service
public class HotelOrderServiceImpl implements IHotelOrderService
{
    @Autowired
    private HotelOrderMapper hotelOrderMapper;

    @Autowired
    private HotelReserveMapper hotelReserveMapper;

    @Autowired
    private IHotelUserService hotelUserService;

    @Autowired
    private IHotelReserveService hotelReserveService;

    /**
     * 查询订单
     *
     * @param id 订单主键
     * @return 订单
     */
    @Override
    public HotelOrder selectHotelOrderById(Long id)
    {
        return hotelOrderMapper.selectHotelOrderById(id);
    }

    /**
     * 查询订单列表
     *
     * @param hotelOrder 订单
     * @return 订单
     */
    @Override
    public List<HotelOrder> selectHotelOrderList(HotelOrder hotelOrder)
    {
        List<HotelOrder> hotelOrders = hotelOrderMapper.selectHotelOrderList(hotelOrder);
        return hotelOrders;
    }

    /**
     * 新增订单
     *
     * @param hotelOrder 订单
     * @return 结果
     */
    @Override
    public int insertHotelOrder(HotelOrder hotelOrder)
    {
        return hotelOrderMapper.insertHotelOrder(hotelOrder);
    }

    /**
     * 修改订单
     *
     * @param hotelOrder 订单
     * @return 结果
     */
    @Override
    @Transactional
    public int updateHotelOrder(HotelOrder hotelOrder)
    {
        Assert.notNull(hotelOrder,"参数为空");
        System.out.println(hotelOrder.getOrderStatus());
        System.out.println(hotelOrder.getUserName());
        if(hotelOrder.getOrderStatus()!=null && hotelOrder.getOrderStatus().equals("3")&& !hotelOrder.getUserName().isEmpty()){
            HotelReserve hotelReserve = hotelReserveService.selectHotelReserveByOrderNumber(hotelOrder.getOrderId());
            Assert.notNull(hotelReserve,"参数异常");
            hotelReserve.setJoinState("3");
            hotelReserveService.updateHotelReserve(hotelReserve);
            System.out.println(hotelReserve.getJoinTime());
            if(new Date().before(hotelReserve.getJoinTime())){
                HotelUser hotelUserQuery = hotelUserService.selectHotelUserByUsername(hotelOrder.getUserName());
                Assert.notNull(hotelUserQuery,"参数异常");
                BigDecimal amount = hotelOrder.getAmount();
                BigDecimal add = hotelUserQuery.getBalance().add(amount);
                hotelUserQuery.setBalance(add);
                hotelUserService.updateHotelUser(hotelUserQuery);
            }
        }
        return hotelOrderMapper.updateHotelOrder(hotelOrder);
    }

    /**
     * 批量删除订单
     *
     * @param ids 需要删除的订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHotelOrderByIds(Long[] ids)
    {
        for (Long id:ids
             ) {
            HotelOrder hotelOrder = hotelOrderMapper.selectHotelOrderById(id);
            Assert.notNull(hotelOrder,"订单不存在");
            HotelReserve hotelReserveQuery = new HotelReserve();
            hotelReserveQuery.setOrderNumber(hotelOrder.getOrderId());
            List<HotelReserve> hotelReserves = hotelReserveMapper.selectHotelReserveList(hotelReserveQuery);
            if(hotelReserves.size()==1){
                hotelReserveMapper.deleteHotelReserveByReserveId(hotelReserves.get(0).getReserveId());
            }
        }
        return hotelOrderMapper.deleteHotelOrderByIds(ids);
    }

    /**
     * 删除订单信息
     *
     * @param id 订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteHotelOrderById(Long id)
    {
        HotelOrder hotelOrder = hotelOrderMapper.selectHotelOrderById(id);
        Assert.notNull(hotelOrder,"没有这个订单");
        HotelReserve hotelReserveQuery = new HotelReserve();
        hotelReserveQuery.setOrderNumber(hotelOrder.getOrderId());
        List<HotelReserve> hotelReserves = hotelReserveMapper.selectHotelReserveList(hotelReserveQuery);
        if(hotelReserves.size()==1){
            hotelReserveMapper.deleteHotelReserveByReserveId(hotelReserves.get(0).getReserveId());
        }
        return hotelOrderMapper.deleteHotelOrderById(id);
    }
    /**
     * 修改订单根据账号
     *
     * @param hotelOrder 订单
     * @return 结果
     */
    public int updateHotelOrderByUserName(HotelOrder hotelOrder){
        return hotelOrderMapper.updateHotelOrderByUserName(hotelOrder);
    }
}
